#include <glib.h>

static void _ptr_array_foreach_print_func(gpointer data, gpointer user_data)
{
    g_print("%s: %s(addr:%p) \n", (gchar *)user_data, (gchar *)data, data);
}

static void _ptr_array_free_func(gpointer data)
{
    if(NULL != data) {
        g_free(data);
    }
}

gint main(gint argc, gchar **argv)
{
    GPtrArray *parr = NULL;
    guint idx = 0;
    gboolean ret = FALSE;
    gchar str0[8] = {0};
    gchar str1[8] = {0};
    gchar str2[8] = {0};
    gchar str3[8] = {0};
    gchar str4[8] = {0};
    gchar str5[8] = {0};
    gchar str6[8] = {0};
    gchar *str[8] = {NULL};
    
    g_strlcpy(str0, "one", 8);
    g_strlcpy(str1, "two", 8);
    g_strlcpy(str2, "two", 8);
    g_strlcpy(str3, "three", 8);
    g_strlcpy(str4, "four", 8);
    g_strlcpy(str5, "five", 8);
    g_strlcpy(str6, "six", 8);

    str[0] = g_strdup(str0);
    str[1] = g_strdup(str1);
    str[2] = g_strdup(str2);
    str[3] = g_strdup(str3);
    str[4] = g_strdup(str4);
    str[5] = g_strdup(str5);
    str[6] = g_strdup(str6);

    parr = g_ptr_array_sized_new(10);
    
    g_ptr_array_add(parr, (gpointer)str[0]);
    g_ptr_array_add(parr, (gpointer)str[1]);
    g_ptr_array_add(parr, (gpointer)str[2]);
    g_ptr_array_add(parr, (gpointer)str[3]);
    g_ptr_array_add(parr, (gpointer)str[4]);
    g_ptr_array_add(parr, (gpointer)str[5]);
    g_ptr_array_add(parr, (gpointer)str[6]);

    g_ptr_array_foreach(parr, _ptr_array_foreach_print_func, "ori");

    g_ptr_array_set_free_func(parr, _ptr_array_free_func);

    ret = g_ptr_array_find(parr, str[3], NULL);
    g_print("find three? %s \n", ret ? "TRUE" : "FALSE");

    ret = g_ptr_array_find(parr, str[4], &idx);
    g_print("find four? %s, pos:%d \n", ret ? "TRUE" : "FALSE", idx);

    g_ptr_array_free(parr, TRUE);

    return 0;
}